package org.swqsbssddqs.shortlink.admin.common.fliterException;

import jakarta.servlet.*;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class ExceptionHandlerFilterRegistrationBean extends FilterRegistrationBean<Filter>{
    @Override
    public Filter getFilter() {
        setUrlPatterns(List.of("/*"));
        setOrder(Integer.MIN_VALUE);
        return new ExceptionHandlerFilter();
    }

    class ExceptionHandlerFilter implements Filter {
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            try {
                chain.doFilter(request, response);
            } catch (Exception e) {
                // 异常捕获，发送到error controller
                request.setAttribute("filter.error", e);
                //将异常分发到/error/exThrow控制器
                request.getRequestDispatcher("/error/exThrow").forward(request, response);
            }
        }

    }
}









